package com.day5.ioc;

import domain.Actor;
import service.ioc.ActorService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public class ActorController {
    private ActorService actorService = null;

    protected String index(HttpServletRequest req) {

        HttpSession session = req.getSession();
        String keyword = null;
        int pageNo = 1;
        String oper = req.getParameter("oper");
        if (oper != null && !"".equals(oper)) {
            keyword = req.getParameter("keyword");
            pageNo = 1;
            if (keyword == null || "".equals(keyword)) {
                keyword = "";
            }
            session.setAttribute("keyword", keyword);
        } else {
            String pageNoStr = req.getParameter("pageNo");
            if (pageNoStr != null && !"".equals(pageNoStr)) {
                pageNo = Integer.parseInt(pageNoStr);
            }
            Object keywordObj = session.getAttribute("keyword");
            if (keywordObj == null) {
                keyword = "";
            } else {
                keyword = (String) keywordObj;
            }
        }

        ActorService actorService = new ActorService();
        List<Actor> actorList = actorService.getList(pageNo,keyword);
        int total = actorService.getTotal(keyword);
        int totalPage = (total + 5 - 1) / 5;
        session.setAttribute("actorList",actorList);
        session.setAttribute("currentPageNo",pageNo);
        session.setAttribute("totalPageNo", totalPage);

        return "index";

    }

    protected String edit(HttpServletRequest req) {
        String idStr = req.getParameter("id");
        if (idStr != null && !"".equals(idStr)) {
            int id = Integer.parseInt(idStr);
            Actor info = actorService.getInfo(id);
            req.setAttribute("actor", info);
            return "edit";
        }
        return "error";
    }

    protected String  delete(HttpServletRequest req) {

        String idStr = req.getParameter("id");
        int id = Integer.parseInt(idStr);
        boolean delete = actorService.delete(id);
        if (delete) {
            return "redirect:list";
        } else {
            return "error";
        }

    }

    protected String save(HttpServletRequest req) {

        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");

        boolean add = actorService.add(name, sex, phone);
        if (add) {
            return "redirect:list";
        } else {
            return "error";
        }

    }

    protected String update(HttpServletRequest req) {

        String idStr = req.getParameter("id");
        int id = Integer.parseInt(idStr);
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");

        boolean res = actorService.update(id, name, sex, phone);
        if (res) {
            return "redirect:list";
        } else {
            return "error";
        }
    }
}
